﻿#ifndef PST_POSTIVEDOUBLESPINBOX_H
#define PST_POSTIVEDOUBLESPINBOX_H

#include "API_BaseWidget.h"
#include "DoubleSpinBox.h"

namespace BaseWidget
{
    class API_BASEWIDGET PostiveDoubleSpinBox : public DoubleSpinBox
    {
        Q_OBJECT

    public:
        explicit PostiveDoubleSpinBox(QWidget* pQParent = nullptr) : DoubleSpinBox(pQParent)
        {
        }
        virtual ~PostiveDoubleSpinBox() = default;
        PostiveDoubleSpinBox(const PostiveDoubleSpinBox&) = delete;
        PostiveDoubleSpinBox& operator=(const PostiveDoubleSpinBox&) = delete;

    protected:
        virtual QValidator::State validate(QString& text, int& pos) const override
        {
            if (text.isEmpty())
            {
                return QValidator::Acceptable;
            }
            if (text.startsWith("-"))
            {
                return QValidator::Invalid;
            }
            if (text.contains(",") || text.contains("-") || text.contains("e"))
            {
                return QValidator::Acceptable;
            }
            bool ok = false;
            locale().toDouble(text, &ok);
            return ok ? QValidator::Acceptable : QValidator::Invalid;
        }

    };

    
} // namespace BaseWidget
#endif
